package com.hcinfos.gsoo.service;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.hcinfos.gsoo.GsooApplication;
import com.hcinfos.gsoo.util.LogUtil;

import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;

public class LocationService extends Service implements AMapLocationListener,
		Runnable {

	private AMapLocation aMapLocation;// 用于判断定位超时
	private LocationManagerProxy aMapLocManager = null;
	private boolean isLocation = true;
	public final static String LOCATION_ACTION = "com.gsoo.location.action";
	private final int LOCATION_SUCCESS = 0x2210;
	private final int LOCATION_FAIL = LOCATION_SUCCESS + 1;

	private boolean isStart = false;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();

		LogUtil.d("wxf", "location Service  onCreate..............");
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);

		//requestLocation();
		LogUtil.d("wxf", "location Service  onStart..............");

	}

	@Override
	public void onDestroy() {
		stopLocation();
		super.onDestroy();
		LogUtil.d("wxf", "location Service  onDestroy..............");

	}

	@Override
	public void onLocationChanged(Location location) {

	}

	@Override
	public void onProviderDisabled(String provider) {

	}

	@Override
	public void onProviderEnabled(String provider) {

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {

	}

	private void requestLocation() {
		isLocation = true;
		aMapLocManager = LocationManagerProxy.getInstance(this);
		aMapLocManager.setGpsEnable(false);
		aMapLocManager.requestLocationUpdates(
				LocationProviderProxy.AMapNetwork, 2000, 10, this);
		handler.postDelayed(this, 12000);// 设置超过12秒还没有定位到就停止定位
	}

	@Override
	public void run() {
		if (aMapLocation == null) {
			stopLocation();
		}
	}

	private void stopLocation() {
		try {
			if (aMapLocManager != null) {
				aMapLocManager.removeUpdates(this);
				aMapLocManager.destory();
			}
			aMapLocManager = null;
		} catch (Exception e) {
			LogUtil.e("wxf", "stopLocation = " + e.toString());
		}

	}

	@Override
	public void onLocationChanged(AMapLocation location) {
		LogUtil.d("wxf", "location .......");
		if (location != null) {
			this.aMapLocation = location;// 判断超时机制
			GsooApplication.lat = location.getLatitude();
			GsooApplication.lng = location.getLongitude();
			GsooApplication.cityName = location.getCity();
			Bundle locBundle = location.getExtras();
			if (locBundle != null) {
				GsooApplication.address = locBundle.getString("desc");
			}
			if (isLocation) {
				isLocation = false;
				// send brocast
				handler.sendEmptyMessage(LOCATION_SUCCESS);
			}
		} else {
			LogUtil.d("wxf", "service  location is null.........");
			handler.sendEmptyMessage(LOCATION_FAIL);
			LogUtil.e("wxf", "location is error  375");
		}
	}

	Handler handler = new Handler() {

		public void handleMessage(android.os.Message msg) {

			isStart = true;
			switch (msg.what) {
			case LOCATION_SUCCESS:
				Intent intent = new Intent(LOCATION_ACTION);
				sendBroadcast(intent);
				stopLocation();
				stopSelf();
				break;
			case LOCATION_FAIL:
				stopLocation();
				stopSelf();
				break;
			}

		};
	};

}
